import base64

from django.shortcuts import render

# Create your views here.
from rest_framework.generics import CreateAPIView,RetrieveAPIView, UpdateAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from resume import serializers
from resume.models import Resume

from users.utils import storage


class ResumeCrerteView(CreateAPIView):
    """
    生成简历
    """
    serializer_class = serializers.CreateResumeSerializer


class ResumeDetailView(RetrieveAPIView):
    """
    当前用户简历详细信息
    """

    def get(self, request, *args, **kwargs):
        pk = self.kwargs['pk']
        data = serializers.UserResumeDetailSerializer(Resume.objects.filter(user=pk)[0])
        return Response(data.data)



class UserResumeUpdateUserView(UpdateAPIView):
    """
    修改用户数据
    """
    serializer_class = serializers.UserResumeUpdateSerializer
    permission_classes = [IsAuthenticated]

    def get_object(self, *args, **kwargs):
        if self.request.data.get('avatar_base64'):
            url = storage(base64.b64decode(self.request.data['avatar_base64'].split(',')[1]))
            self.request.user.avatar_url = 'http://pns4r9vpo.bkt.clouddn.com/' + url
        return self.request.user